from business.business_base import BusinessBase


class InUpgradeBusiness(BusinessBase):
    """
    升级过程中执行业务
    """

    def __init__(self, **kwargs):
        super(InUpgradeBusiness, self).__init__(**kwargs)
        self.db_b_name_in = 'db_testb_in'
        self.t_b_name_in = 't_testb_in'
        self.up_user_prefix = 'upgrade_'
        self.tbs_name_in = 'tps_index_in'
        self.t_index_in = 't_index_in'
        self.index_name_in = 'i_index_in'

    def during_run_compatibility_b(self):
        """
        灰度升级过程中，执行B库相关的业务:建B库，B库下dml
        :return: 无
        """
        if int(self.up_db_v[0].replace('.', '')) != 300:
            self.log.info('灰度升级中-执行B库相关的业务')
            self.check_db_compatibility_b(self.db_b_name_in, self.t_b_name_in)

    def during_run_tpcc(self):
        """
        灰度升级过程中，执行tpcc业务
        :return:无
        """
        self.log.info('灰度升级中-执行tpcc业务')
        self.run_tpcc('run')

    def during_check_index(self):
        """
        灰度升级过程中，创建/修改/删除索引及分区表全局索引和分区索引业务
        :return: 无
        """
        self.log.info('灰度升级中-执行索引相关业务')
        self.index(self.tbs_name_in, self.t_index_in, self.index_name_in)

    def during_run_row_compress(self):
        """
        灰度升级过程中, 执行行存压缩、分区表相关业务
        :return: 无
        """
        if int(self.up_db_v[0].replace('.', '')) >= 300:
            self.log.info('灰度升级中-执行行存压缩、分区表相关业务')
            self.row_compress_dml()

    def during_get_disconnect_time(self):
        """
        灰度升级过程中，获取数据库断连时间
        :return: 返回时间差
        """
        self.init_business_db_data(self.up_user_prefix, self.db_b_name_in)
        time_diff = self.get_disconnect_time()
        return time_diff

    def during_upgrade_business_main(self):
        """
        灰度升级过程中业务执行
        :return: 执行完成后返回True
        """
        self.init_business_db_data(self.up_user_prefix, self.db_b_name_in)
        self.during_run_compatibility_b()
        self.during_run_tpcc()
        self.during_check_index()
        self.during_run_row_compress()
        return True
